package com.huyi.common.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * zookeeper分布式锁
 * 
 * @author shengli.hu
 *
 */
@Configuration
public class CuratorConfig {
	
	public final static String ZOOKEEPER_PATH = "/curator/lock";

	@Value("${curator.zk.address}")
	private String zookeeperAddress;

	@Bean
	public CuratorFramework client() {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zookeeperAddress)
				// 会话超时时间
				.sessionTimeoutMs(5000)
				// 连接超时时间
				.connectionTimeoutMs(5000)
				.retryPolicy(retryPolicy).build();
		client.start();
		return client;
	}

}
